<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2020 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://demo.thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\cms\controller;

/**
 * 栏目管理
 */
class Pay extends BaseController
{

    /**
     * 当前操作数据库
     */
    public $table = 'CmsCarPay';
    public $privacy = array(0=>"已付款",1=>"未付款",2=>"未付全款");
    public $repairs = array(0=>"机油保养",1=>"机修",2=>"美容",3=>"其他");


    /**
     * 栏目管理
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        $this->title = "货款列表";
        $this->supplier = $this->app->request->request("supplier");
        $this->nickname = $this->app->request->request("nickname");
        $map = [];
        $query = $this->_query($this->table)->like('supplier')->where($map);
        if($this->nickname){
            $openids = $this->_get_openids_by_nickname($this->nickname);
            $query = $query->whereIn("openid", $openids);
        }

        $query->order('id desc')->page();
    }

    protected function _get_openids_by_nickname($nickname){
        $openids = [];
        $users = $this->app->db->name('CmsCarUser')->field("pid,nickname")
            ->whereLike("nickname","%$nickname%")->select();
        //echo $this->app->db->name('CmsCarUser')->getLastSql();
        for($i=0; $i<count($users); $i++){
            $openids[] = $users[$i]['pid'];
        }
        return $openids;
    }

    protected function _index_page_filter(&$data)
    {

        $class_ids = $this->_getDataIds($data, "openid");
        $class = $this->_getClassDict($class_ids);
        for($i=0; $i<count($data); $i++){
            $data[$i]['nickname'] = $class[$data[$i]['openid']];
        }

    }



    protected function _getClassDict($class_ids){
        $map = array("pid"=> $class_ids);
        $class = $this->app->db->name('CmsCarUser')->field("pid,nickname")->where($map)->select();
        $result = array();
        for ($i=0; $i<count($class); $i++){
            $result[$class[$i]['pid']] = $class[$i]['nickname'];
        }
        return $result;
    }

    protected function _getDataIds($data, $key){
        $arr = [];
        for ($i=0; $i<count($data); $i++){
            array_push($arr, $data[$i][$key]);
        }
        return $arr;
    }

    /**
     * 添加项目
     */
    public function add()
    {

        if ($this->request->isGet()) {
            $this->_form($this->table, 'form');
        } else {
            $repair=$this->request->post('repair');
            $img=$this->request->post('img');

           //$imgs=implode(',',$img);
           // var_dump( $imgs);
            $repairs = implode(',',$repair);
//var_dump( $repair);

            $update = [
                'number' => $this->request->post('number'),
                "mileage" => $this->request->post('mileage'),
                "remarks" => $this->request->post('remarks'),
                'repair' => $repairs,
                'content' => $this->request->post('content'),
                'img' => $img,
                'status' => $this->request->post('status'),
                'paid' => $this->request->post('paid'),
                'total' =>$this->request->post('total'),
                'start_at'=>date('Y-m-d', time())
            ];
            if ($this->app->db->name($this->table)->insert($update) !== false) {
                $this->success('添加成功！');
            } else {
                $this->error('添加失败，请稍候再试！');
            }
        }
    }

    /**
     * 查询
     */
    public function queryInfo()
    {
        if ($this->request->isGet()) {
            $this->verify = false;
            $this->_form($this->table, 'form');
        } else {
            $this->number = $this->request->post('number');
            $this->id = $this->request->post('id');
            $map = ["id" =>$this->id];
            $query = $this->app->db->name('CmsCarWorks')
               ->where($map)
                ->order('id desc')->select()->toArray();

            $this->success($query);

        }
    }


    /**
     * 查询
     */
    public function queryList()
    {
        if ($this->request->isGet()) {
            $this->verify = false;
            $this->_form($this->table, 'form');
        } else {

            $this->number=$this->request->post('number');
            $this->repair=$this->request->post('repair');
            $this->status=$this->request->post('status');
            $this->start_at=$this->request->post('start_at');
            $this->end_at=$this->request->post('end_at');
            if($this->end_at===""){
                $this->end_at=date('Y-m-d', time());
            }
            //strtotime(start_at);

            //$where[] = ['start_at', 'end_at', [$this->start_at,  $this->end_at]];
            $query =$this->app->db->name('CmsCarWorks')
                ->whereLike("number","%".$this->number."%")
                ->whereLike("repair","%".$this->repair."%")
                ->whereLike("status","%".$this->status."%")
                ->whereBetweenTime(" start_at",$this->start_at,$this->end_at)

                ->order('id desc')->select()->toArray();

            $this->success($query);

        }
    }
 public function setUpdate(){
     if ($this->request->isGet()) {
         $this->_form($this->table, 'form');
     } else {

         $id=$this->request->post('id');
         $map = array("id" => $id);
         $update = [
             'cost' =>$this->request->post('cost'),
             'explain' =>$this->request->post('explain')
         ];

         if ($this->app->db->name($this->table)->where($map)->update($update) !== false) {
             $this->success('录入成功！');
         } else {
             $this->error('录入失败，请稍候再试！');
         }
     }

 }


    /**
     * 编辑栏目
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit()
    {

        $this->id = $this->request->request('id');
        $map = array("id" => $this->id);
        if (empty($this->id)) $this->error('参数错误，请稍候再试！');
        if ($this->request->isGet()) {
            if ($this->request->get('output') === 'json') {

                $this->success('获取数据成功！', $this->app->db->name($this->table)->where($map)->find());

            } else {
                $this->title = '编辑信息';
                $this->_applyFormToken();
                $this->_form($this->table, 'form');
            }
        }else{
            $data = $this->request->post();
            //$this->error($data);
            if ($this->app->db->name($this->table)->where($map)->update($data) !== false) {
                $this->success('信息更新成功！', 'javascript:history.back()');
            } else {
                $this->error('信息更新失败，请稍候再试！');
            }
        }

    }




    /**
     * 删除栏目
     * @auth true
     * @throws \think\db\exception\DbException
     */
    public function remove()
    {
        $this->_applyFormToken();
        $this->id= $this->request->post('id');
        $map = ['id' =>$this->id];
        $this->app->db->name($this->table)->where($map)->delete();
        $this->_delete($this->table);
    }



}
